package org.wattdepot.gdata.kukuicup.updater.energygoalgame;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

/**
 * Provides access to the user data file written out by Makahiki to support beta evaluation.
 * @author Philip Johnson
 */
public class BetaEvalUserData {

  /* The name of the file that is written out periodically with user data. */    
  private String fileName;
  
  /* The map containing all user data. */
  private Map<String, Integer[]> loungeData = new HashMap<String, Integer[]>();
  
  /**
   * Creates a BetaEvalUserData instance whose getCurrentUserData method can be called 
   * periodically to get the last user data written for the beta tests.  
   * @param fileName The name of the file where the user data is held.
   */
  public BetaEvalUserData(String fileName) {
    this.fileName = fileName;
    this.loungeData = getCurrentLoungeData();
  }
  
  /**
   * Returns the total points for this lounge so far. 
   * @param lounge The lounge. 
   * @return It's point total.
   */
  public int getTotalPoints(String lounge) {
    return getLoungeData(lounge, 0);
  }
  
  /**
   * Returns the total points for this lounge so far. 
   * @param lounge The lounge. 
   * @return It's point total.
   */
  public int getUserPosts(String lounge) {
    return getLoungeData(lounge, 1);
  }
  
  /**
   * Returns the total points for this lounge so far. 
   * @param lounge The lounge. 
   * @return It's point total.
   */
  public int getCanopyMembers(String lounge) {
    return getLoungeData(lounge, 2);
  }
  
  /**
   * Returns the specified data for this lounge.
   * @param lounge The lounge. 
   * @param index The index value for the desired data.
   * @return It's point total.
   */
  private int getLoungeData(String lounge, int index) {
    if (!this.loungeData.containsKey(lounge)) {
      throw new RuntimeException("Tried to get data for nonexistent lounge.");
    }
    return this.loungeData.get(lounge)[index];
  }
  
  /**
   * Returns a map whose keys are the lounge names ("Lehua-A", etc.) and whose values are 
   * the user data associated with this lounge. These data are, in order:
   * <ol>
   * <li> Total floor points so far;
   * <li> Number of user posts so far;
   * <li> Number of canopy members so far;
   * </ol>
   * @return Map of user data for each lounge.
   */
  private Map<String, Integer[]> getCurrentLoungeData() {
    
    Map<String, Integer[]> data = new HashMap<String, Integer[]>();
    BufferedReader br = null;
  
    try {
      br = new BufferedReader( new FileReader(fileName));
      String lineOfText; 
      while ((lineOfText = br.readLine()) != null) {
        // Get the tokens for each line.
        StringTokenizer tokens = new StringTokenizer(lineOfText, ",");
        String dorm = tokens.nextToken();
        String loungeLetter = tokens.nextToken();
        Integer floorPoints = Integer.parseInt(tokens.nextToken());
        Integer userPosts = Integer.parseInt(tokens.nextToken());
        Integer canopyMembers = Integer.parseInt(tokens.nextToken());
        // Update the data structure
        Integer[] numbers = { floorPoints, userPosts, canopyMembers };
        data.put(dorm + "-" + loungeLetter, numbers);
      }
    } 
    catch (Exception e) {
      e.printStackTrace();
    }
    finally {
      try {
        br.close();
      }
      catch (IOException e) {
        e.printStackTrace();
      }
    }
    // return the data structure
    return data;
  }
}
